package dao.entities.control;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import utils.EncryptionUtils;
import utils.SessionUtils;
import views.annotations.Buscar;
import views.annotations.View;
import dao.interfaces.IBase;

@Entity
@Table(schema = "GENERAL", name = "USER")
@View.Views({ @View(name = "table", fields = ""),
		@View(name = "record", fields = "description,password,notes,profile") })
@Buscar(fields = "description,notes,profile")
public class User implements IBase {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@Column(unique = true)
	@NotEmpty
	private String description;

	// debe contener almenos un digito
	@Pattern(regexp = ".*\\d.*", message = "almenos un digito")
	@Length(min = 8)
	@NotEmpty
	private String password;

	@Length(min = 2, max = 24)
	private String notes;

	@Enumerated(EnumType.STRING)
	private Profile profile;

	@NotNull
	private String user;
	private Date date;

	@PreUpdate
	@PrePersist
	private void updateSave() {
		if (!password.matches("[a-fA-F0-9]{32}")) {
			password = EncryptionUtils.ConvertToMD5(password);
		}
		date = new Date();
		user = (String) SessionUtils.getSessionAttribute("user");
	}

	public enum Profile {
		apuntador, master, apuntadorLight,apuntadorSemi
	}

	@Override
	public Object getId() {
		return id;
	}

	@Override
	public void setId(Object id) {
		this.id = (long) id;
	};

	@Override
	public String getDescription() {
		return description;
	};

	@Override
	public void setDescription(String description) {
		this.description = description;
	}

	public String getPassword() {
		return password;
	};

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String getNotes() {
		return notes;
	};

	@Override
	public void setNotes(String notes) {
		this.notes = notes;
	}

	@Override
	public String getUser() {
		return user;
	};

	@Override
	public void setUser(String user) {
		this.user = user;
	}

	@Override
	public Date getDate() {
		return date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return description;
	};

	public User() {
		super();
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

}
